<?php
namespace Concurso\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Concurso\Model\Concurso;          // <-- Add this import
use Concurso\Form\ConcursoForm;       // <-- Add this import

class ConcursoController extends AbstractActionController
{
 // Add content to this method:
    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('concurso', array(
                'action' => 'add'
            ));
        }

        // Get the Album with the specified id.  An exception is thrown
        // if it cannot be found, in which case go to the index page.
        try {
            $concurso = $this->getConcursoTable()->getConcurso($id);
        }
        catch (\Exception $ex) {
            return $this->redirect()->toRoute('concurso', array(
                'action' => 'index'
            ));
        }

        $form  = new ConcursoForm();
        $form->bind($concurso);
        $form->get('submit')->setAttribute('value', 'Edit');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setInputFilter($concurso->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getConcursoTable()->saveConcurso($form->getData());

                // Redirect to list of albums
                return $this->redirect()->toRoute('concurso');
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }
    
    // Add content to the following method:
    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('concurso');
        }

        $request = $this->getRequest();
        if ($request->isPost()) {
            $del = $request->getPost('del', 'No');

            if ($del == 'Yes') {
                $id = (int) $request->getPost('id');
                $this->getConcursoTable()->deleteConcurso($id);
            }

            // Redirect to list of albums
            return $this->redirect()->toRoute('concurso');
        }

        return array(
            'id'    => $id,
            'concurso' => $this->getConcursoTable()->getConcurso($id)
        );
    }
    
     public function addAction()
    {
        $form = new ConcursoForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $concurso = new Concurso();
            $form->setInputFilter($concurso->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $concurso->exchangeArray($form->getData());
                $this->getConcursoTable()->saveConcurso($concurso);

                // Redirect to list of albums
                return $this->redirect()->toRoute('concurso');
            }
        }
        return array('form' => $form);
    }
    
    
    protected $concursoTable;
    public function indexAction()
    {
        return new ViewModel(array(
                    'concursos' => $this->getConcursoTable()->fetchAll(),
                ));
    }
// ...
/*
    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }
 * 
 */
    public function getConcursoTable()
    {
        if (!$this->concursoTable) {
            $sm = $this->getServiceLocator();
            $this->concursoTable = $sm->get('Concurso\Model\ConcursoTable');
        }
        return $this->concursoTable;
    }
}